//
//  NSString+QCExtension.m
//  JotusStripEnd
//
//  Created by 陈立华 on 16/10/8.
//  Copyright © 2016年 陈立华. All rights reserved.
//

#import "NSString+QCExtension.h"

@implementation NSString (QCExtension)

+ (NSString*)convertStringToRed:(CGFloat)red Green:(CGFloat)green Blue:(CGFloat)blue{
    
    NSLog(@"red%d green %d blue %d",(int)(red * 255),(int)(green * 255),(int)(blue * 255));
    NSString *hex = [NSString stringWithFormat:@"%@%@%@",[self ToHex:(int)(red * 255)],[self ToHex:(int)(green * 255)],[self ToHex:(int)(blue * 255)]];
    return hex;
}
//十进制转十六进制
+ (NSString *)ToHex:(int)tmpid
{
    NSString *endtmp=@"";
    NSString *nLetterValue;
    NSString *nStrat;
    int ttmpig=tmpid%16;
    int tmp=tmpid/16;
    switch (ttmpig)
    {
        case 10:
            nLetterValue =@"A";break;
        case 11:
            nLetterValue =@"B";break;
        case 12:
            nLetterValue =@"C";break;
        case 13:
            nLetterValue =@"D";break;
        case 14:
            nLetterValue =@"E";break;
        case 15:
            nLetterValue =@"F";break;
        default:nLetterValue=[[NSString alloc]initWithFormat:@"%i",ttmpig];
            
    }
    switch (tmp)
    {
        case 10:
            nStrat =@"A";break;
        case 11:
            nStrat =@"B";break;
        case 12:
            nStrat =@"C";break;
        case 13:
            nStrat =@"D";break;
        case 14:
            nStrat =@"E";break;
        case 15:
            nStrat =@"F";break;
        default:nStrat=[[NSString alloc]initWithFormat:@"%i",tmp];
            
    }
    endtmp=[[NSString alloc]initWithFormat:@"%@%@",nStrat,nLetterValue];
    return endtmp;
}

// 将数据强制转换，没有拼接01 和03
+ (NSString *)translationWith:(NSString *)dataString
{
    if (dataString.length == 0) {
        return nil;
    }
    
    NSString *result = @"";
    NSString *ch = nil;
    NSString *condition = dataString;
    while (condition.length > 2) {
        ch = [condition substringToIndex:2];
        condition = [condition substringFromIndex:2];
        result = [result stringByAppendingString:[self getData:ch]];
    }
    ch = condition;
    result = [result stringByAppendingString:[self getData:ch]];
    
    return result;
}

// 将数据强制转换,已拼接01 和03
+ (NSString *)finalDataWith:(NSString *)dataString
{
    if (dataString.length == 0) {
        return nil;
    }
    
    NSString *result = @"";
    NSString *ch = nil;
    NSString *condition = dataString;
    while (condition.length > 2) {
        ch = [condition substringToIndex:2];
        condition = [condition substringFromIndex:2];
        result = [result stringByAppendingString:[self getData:ch]];
    }
    ch = condition;
    result = [result stringByAppendingString:[self getData:ch]];
    
    NSString *finalString = [@"01" stringByAppendingString:result];
    finalString = [finalString stringByAppendingString:@"03"];
    
    return finalString;
}

// 判断字符串是否包含中文
+ (BOOL)hasChinese:(NSString *)originString
{
    for (int i=0; i<originString.length; i++) {
        unichar ch = [originString characterAtIndex:i];
        if (0x4E00 <= ch  && ch <= 0x9FA5) {
            return YES;
        }
    }
    return NO;
}


+ (NSString *)getData:(NSString *)ch
{
    NSString *outString = @"";
    int origin = (int)[NSString numberWithHexString:ch];
    if (origin > 0x00 && origin < 0x04) {
        outString = [outString stringByAppendingString:@"02"];
        origin ^= 0x04;
    }
    outString = [outString stringByAppendingString:[NSString ToHex:origin]];
    
    return outString;
}

+ (NSInteger)numberWithHexString:(NSString *)hexString{
    
    const char *hexChar = [hexString cStringUsingEncoding:NSUTF8StringEncoding];
    
    int hexNumber;
    
    sscanf(hexChar, "%x", &hexNumber);
    
    return (NSInteger)hexNumber;
}



@end
